package tv.youxing.android.custom;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import tv.youxing.android.Interface.CustomToobarImp;
import tv.youxing.android.R;


/**
 * Created by Administrator on 2017/5/25.
 */

public class CustomToolbar extends RelativeLayout {




    int  iv_back_resources_id;
    String tv_back_title_str;
    String title;
    String right_text;
    int right_icon_res;


    ImageView iv_back;
    TextView tv_back_title;//回退以文字的方式显示
    TextView tv_title;
    TextView tv_right_text;
    ImageView iv_right_icon;
    View rootView;

    boolean back_is_show;//是否显示返回按钮
    boolean title_back_is_show;
    boolean right_text_is_show;//是否显示返回按钮
    boolean right_icon_is_show;//是否显示返回按钮

    Context mContext;


    public CustomToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyToolbar);

        rootView = View.inflate(context, R.layout.costom_toolbar, null);


        iv_back = (ImageView) rootView.findViewById(R.id.ic_back);
        tv_back_title = rootView.findViewById(R.id.tv_back);
        tv_title = (TextView) rootView.findViewById(R.id.title);
        tv_right_text = rootView.findViewById(R.id.right_textview);



        title_back_is_show = ta.getBoolean(R.styleable.MyToolbar_tv_back_title_is_show,false);
        back_is_show = ta.getBoolean(R.styleable.MyToolbar_back_is_show, true);
        right_text_is_show = ta.getBoolean(R.styleable.MyToolbar_right_text_is_show, false);
        right_icon_is_show = ta.getBoolean(R.styleable.MyToolbar_right_iocn_is_show, false);



        iv_back_resources_id = ta.getResourceId(R.styleable.MyToolbar_iv_back_img_resources,R.drawable.icon_more_left);
        tv_back_title_str = ta.getString(R.styleable.MyToolbar_tv_back_title);
        title = ta.getString(R.styleable.MyToolbar_title);
        right_text = ta.getString(R.styleable.MyToolbar_right_text);
        right_icon_res = ta.getInt(R.styleable.MyToolbar_right_icon,R.drawable.nav_share);

        ta.recycle();


        initView();


    }

    private void initView() {

        final Activity activity = (Activity) mContext;

        if (back_is_show) {
            iv_back.setVisibility(VISIBLE);
            iv_back.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    activity.finish();
                }
            });
            tv_back_title.setVisibility(GONE);
        } else {
            iv_back.setVisibility(GONE);
        }


        if (title_back_is_show) {
            tv_back_title.setVisibility(VISIBLE);
            iv_back.setVisibility(GONE);
            tv_back_title.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    activity.finish();
                }
            });
            tv_back_title.setText(tv_back_title_str);
        } else {
            tv_back_title.setVisibility(GONE);
        }

        if (right_text_is_show) {
            tv_right_text.setVisibility(VISIBLE);
            tv_right_text.setText(right_text);
        } else {
            tv_right_text.setVisibility(GONE);
        }



        ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, R.dimen.custom_toolbar
        );

        rootView.setLayoutParams(layoutParams);

        tv_title.setText(title);
        addView(rootView);
    }


    public void setBackClickLister(final CustomToobarImp.back backClickLister) {
        iv_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                backClickLister.onclick();
            }
        });
    }

    public void setRightTextClickLister(final CustomToobarImp.right_text clickLister) {
        tv_right_text.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                clickLister.onclick();
            }
        });
    }


    public void setTitle(String title) {
        this.tv_title.setText(title);
    }



}
